<!DOCTYPE html>
<html>
<head>
    <title>地址编辑</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <link rel="stylesheet" href="../lib/weui.min.css">
    <link rel="stylesheet" href="../css/jquery-weui.css">
    <link rel="stylesheet" href="../css/base.css">
    <link rel="stylesheet" href="../css/addAddress.css">
</head>
<body>
<div class="xyg-address weui-cells weui-cells_form">
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">收货人</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" type="text" id="name" name="name" value="" placeholder="请输入收货人">
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd">
            <label class="weui-label">手机号</label>
        </div>
        <div class="weui-cell__bd">
            <input class="weui-input" type="tel" id="phone" name="phone" value="" placeholder="请输入手机号">
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd">
            <label class="weui-label">选择地区</label>
        </div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="address" type="text" value="" placeholder="请选择地区">
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd">
            <label class="weui-label">详细地址</label>
        </div>
        <div class="weui-cell__bd">
            <input class="weui-input" type="text" id="descripts" value="" placeholder="请输入详细地址">
        </div>
    </div>
    <div class="weui-cell weui-cell_switch">
        <div class="weui-cell__bd">默认地址</div>
        <div class="weui-cell__ft">
            <input class="weui-switch" id="type" name="type" type="checkbox">
            <input hidden="hidden" id="btn" value="off" >
        </div>
    </div>
</div>
<div class="weui-btn-area">
    <a class="weui-btn weui-btn_default" href="javascript:void(0)" id="showTooltips">确定</a>
</div>
</body>
<script src="../lib/jquery-2.1.4.js"></script>
<script src="../js/jquery-weui.js"></script>
<script src="../js/city-picker.js" charset="utf-8"></script>
<script src="../fonts/iconfont.js"></script>
<script src="../lib/handlebars.min.js"></script>
<script src="../js/config.js"></script>
<script>
    var memberId = getQueryString("memberId");
    var orderId = getQueryString("orderId");
    var orderItemJson = getQueryString("orderItemJson");
    var selectType = getQueryString("type");
    var addressId = getQueryString("addressId");

    $("#address").cityPicker({
        title: "请选择收货地址"
    });

    if(addressId){
        // 查询info
        $.get(baseURL + "api/address/default?memberId="+memberId+"&addressId="+addressId,"",function(res) {
            if (res.code == 0) {
                var address = res.address;
                $("#name").val(address.name);
                $("#phone").val(address.phone);
                $("#address").val(address.province);
                $("#descripts").val(address.descripts);
                //console.log(address.type)
                if(address.type == "2"){
                    $("#type").attr("checked","checked");
                    $("#btn").attr("value","2");
                }else{
                    $("#type").removeAttr("checked");
                    $("#btn").attr("value","1");
                }
            }
        });
    }

    $("#type").bind("click", function () {
        if ($("#btn").val() == "1") {
            $("#btn").val("2");
            //console.log("开关1当前状态：" + $("#btn").val());
        } else {
            $("#btn").val("1");
            //console.log("开关1当前状态：" + $("#btn").val());
        }
    });

    $("#showTooltips").on("click", function () {
        var name = $("#name").val();
        if (name == null || name == "") {
            $.toast("请输入收货人", "text");
            return false;
        }
        var phone = $("#phone").val();
        if (phone == null || phone == "") {
            $.toast("请输入手机号", "text");
            return false;
        }
        if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
            $.toast("手机号码有误，请重填", "text");
            return false;
        }
        var address = $("#address").val();
        if (address == null || address == "") {
            $.toast("请选择地址", "text");
            return false;
        }
        var descripts = $("#descripts").val();
        if (descripts == null || descripts == "") {
            $.toast("请填写详细地址", "text");
            return false;
        }
        var type = $("#btn").val();
        var param = {
            memberId: memberId,
            name: name,
            phone: phone,
            province: address,
            descripts: descripts,
            type: type
        };
        $.showLoading("正在提交中");
        var url = '';
        if(addressId){
            param.id = addressId;
            url = baseURL + "api/address/update";
        }else{
            url = baseURL + "api/address/save";
        }
        $.post(url, param, function (result) {
            if (result.code == 0) {
                $.hideLoading();
                goAddressAndType();
            } else {
                $.hideLoading();
                $.toast(result.msg, "text");
            }
        }, "json");
    });

    function goAddressAndType(){
        // 正常
        if (selectType == "1") {
            window.location.href="/mobile/view/address.html?memberId="+memberId+"&type="+selectType;
        }
        // 临时订单
        if (selectType == "2"){
            window.location.href="/mobile/view/address.html?memberId="+memberId+"&type="+selectType+
                "&orderItemJson="+encodeURI(orderItemJson);
        }
        // 已生成订单
        if (selectType == "3"){
            window.location.href="/mobile/view/address.html?memberId="+memberId+"&type="+selectType+
                "&orderId="+orderId;
        }
    }
</script>
</html>

